/*
 * ColegiosAdminView.java
 *
 * Created on 24 de diciembre de 2007, 16:32
 */
package jschoolmanagerportable.gui;

import java.awt.Component;
import javax.swing.JOptionPane;
import jschoolmanagerportable.gui.util.Dialogo;
import jschoolmanagerportable.model.Colegio;
import jschoolmanagerportable.model.Curso;

/**
 *
 * @author  Administrador
 */
public class ColegiosAdminView extends javax.swing.JInternalFrame {

    /** Creates new form ColegiosAdminView */
    public ColegiosAdminView() {
        initComponents();
        periodosText.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = javax.persistence.Persistence.createEntityManagerFactory("JSchoolManagerPortablePU").createEntityManager();
        colegioQuery = entityManager.createQuery("SELECT c FROM Colegio c");
        colegioList = org.jdesktop.observablecollections.ObservableCollections.observableList(colegioQuery.getResultList());
        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaColegios = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        idText = new javax.swing.JTextField();
        nombreText = new javax.swing.JTextField();
        descripcionText = new javax.swing.JTextField();
        botonGuardar = new javax.swing.JButton();
        botonBorrar = new javax.swing.JButton();
        botonNuevo = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        semestral = new javax.swing.JRadioButton();
        trimestral = new javax.swing.JRadioButton();
        periodosText = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jschoolmanagerportable.JSchoolManagerPortableApp.class).getContext().getResourceMap(ColegiosAdminView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaColegios.setName("tablaColegios"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, colegioList, tablaColegios);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Nombre");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${descripcion}"));
        columnBinding.setColumnName("Descripcion");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${periodos}"));
        columnBinding.setColumnName("Periodos");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(tablaColegios);
        tablaColegios.getColumnModel().getColumn(0).setResizable(false);
        tablaColegios.getColumnModel().getColumn(0).setPreferredWidth(20);
        tablaColegios.getColumnModel().getColumn(1).setPreferredWidth(100);
        tablaColegios.getColumnModel().getColumn(2).setPreferredWidth(200);
        tablaColegios.getColumnModel().getColumn(3).setPreferredWidth(20);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        idText.setEditable(false);
        idText.setName("idText"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaColegios, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.id}"), idText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        nombreText.setName("nombreText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaColegios, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.nombre}"), nombreText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        descripcionText.setName("descripcionText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaColegios, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.descripcion}"), descripcionText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        botonGuardar.setText(resourceMap.getString("botonGuardar.text")); // NOI18N
        botonGuardar.setName("botonGuardar"); // NOI18N
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonBorrar.setText(resourceMap.getString("botonBorrar.text")); // NOI18N
        botonBorrar.setName("botonBorrar"); // NOI18N
        botonBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarActionPerformed(evt);
            }
        });

        botonNuevo.setText(resourceMap.getString("botonNuevo.text")); // NOI18N
        botonNuevo.setName("botonNuevo"); // NOI18N
        botonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNuevoActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        buttonGroup1.add(semestral);
        semestral.setText(resourceMap.getString("semestral.text")); // NOI18N
        semestral.setName("semestral"); // NOI18N
        semestral.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                semestralActionPerformed(evt);
            }
        });

        buttonGroup1.add(trimestral);
        trimestral.setSelected(true);
        trimestral.setText(resourceMap.getString("trimestral.text")); // NOI18N
        trimestral.setName("trimestral"); // NOI18N
        trimestral.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trimestralActionPerformed(evt);
            }
        });

        periodosText.setEditable(false);
        periodosText.setEnabled(false);
        periodosText.setName("periodosText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tablaColegios, org.jdesktop.beansbinding.ELProperty.create("${selectedElement.periodos}"), periodosText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(80, 80, 80)
                        .addComponent(botonGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBorrar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonNuevo))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 551, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(descripcionText, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                                    .addComponent(nombreText, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(idText, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(trimestral)
                        .addGap(18, 18, 18)
                        .addComponent(semestral)
                        .addGap(34, 34, 34)
                        .addComponent(periodosText, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonBorrar, botonGuardar, botonNuevo});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(idText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nombreText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(descripcionText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(trimestral)
                    .addComponent(semestral)
                    .addComponent(periodosText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonGuardar)
                    .addComponent(botonBorrar)
                    .addComponent(botonNuevo))
                .addContainerGap())
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed

        if (idText.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar un registro de la tabla");
            return;
        }

        Colegio colegio = new Colegio();
        colegio.setId(Integer.parseInt(idText.getText()));
        colegio.setNombre(nombreText.getText());
        colegio.setDescripcion(descripcionText.getText());
        colegio.setPeriodos(Integer.parseInt(periodosText.getText()));
        entityManager.getTransaction().begin();
        entityManager.merge(colegio);
        entityManager.getTransaction().commit();
        idText.setText(String.valueOf(colegio.getId()));
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarActionPerformed
        Colegio colegio = new Colegio();
        colegio.setId(Integer.parseInt(idText.getText()));
        colegio.setNombre(nombreText.getText());
        colegio.setDescripcion(descripcionText.getText());
        try {
            entityManager.getTransaction().begin();
            colegio = entityManager.merge(colegio);
            entityManager.remove(colegio);
            entityManager.getTransaction().commit();
            colegioList.remove(colegio);
            int row = colegioList.size() - 1;
            if (row != -1) {
                tablaColegios.setRowSelectionInterval(row, row);
                tablaColegios.scrollRectToVisible(tablaColegios.getCellRect(row, 0, true));
            }
            tablaColegios.repaint();
        } catch (Exception e) {
            Dialogo.alert("Error", "No se pudo borrar el Colegio", (Component) evt.getSource());
        }        
    }//GEN-LAST:event_botonBorrarActionPerformed

    private void botonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNuevoActionPerformed
        Colegio colegio = new Colegio();
        colegio.setNombre("nombre");
        colegio.setDescripcion("descripcion");
        colegio.setPeriodos(2);
        entityManager.getTransaction().begin();
        entityManager.persist(colegio);
        entityManager.getTransaction().commit();
        colegioList.add(colegio);
        int row = colegioList.size() - 1;
        tablaColegios.setRowSelectionInterval(row, row);
        tablaColegios.scrollRectToVisible(tablaColegios.getCellRect(row, 0, true));
        tablaColegios.repaint();
        idText.setText(String.valueOf(colegio.getId()));
        nombreText.requestFocus();
    }//GEN-LAST:event_botonNuevoActionPerformed

    private void trimestralActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trimestralActionPerformed
        periodosText.setText("3");
    }//GEN-LAST:event_trimestralActionPerformed

    private void semestralActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_semestralActionPerformed
        periodosText.setText("2");
    }//GEN-LAST:event_semestralActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonBorrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonNuevo;
    private javax.swing.ButtonGroup buttonGroup1;
    private java.util.List<jschoolmanagerportable.model.Colegio> colegioList;
    private javax.persistence.Query colegioQuery;
    private javax.swing.JTextField descripcionText;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JTextField idText;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField nombreText;
    private javax.swing.JTextField periodosText;
    private javax.swing.JRadioButton semestral;
    private javax.swing.JTable tablaColegios;
    private javax.swing.JRadioButton trimestral;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
